# encoding=utf-8
from LBT.BaseParent.BaseLbt import lbtlogin
from LBT.Resource.SchoolStationAtt import SchoolStationAtt
import time
from bs4 import BeautifulSoup


class SchoolDynamic(lbtlogin, SchoolStationAtt):
    """园所动态模块
    1、新增园所动态并数据库校验，然后删除并校验
    2、编辑园所动态并页面校验，然后改回来
    """

    @lbtlogin.loginFun
    def test_adddynamic(self):
        """
        新增园所动态并数据库校验，然后删除并校验
        :return:
        """
        self.docstr ='新增园所动态并数据库校验，然后删除并校验'
        dr = self.driver
        self.moduleOp.openMoudleAndIn(self.schoolstation_navigation_class, self.schoolstation_dynamic_url)
        self.click_element_by_href(dr, self.schoolstation_dynamic_publishdynamicbtn_href)
        """新增一个新闻，并校验"""
        newtitle = "新闻标题" + self.getCurrentTime()
        self.clearAndSendKeyForText(dr.find_element_by_css_selector(self.schoolstation_dynamic_publish_title_css),newtitle,dr)
        dr.find_element_by_xpath(self.schoolstation_dynamic_publish_huodong_xpath).click()
        dr.find_element_by_id(self.schoolstation_dynamic_publish_addphoto_id).click()
        time.sleep(3)
        self.uploadImg(3, __file__)
        dr.switch_to.frame(self.schoolstation_dynamic_textframe_id)
        newtext = "abcdef"
        dr.find_element_by_tag_name("body").send_keys(newtext)
        dr.switch_to.parent_frame()
        dr.find_element_by_xpath(self.schoolstation_dynamic_publish_publish_xpath).click()
        sqltitle = "select top 1 title from C_SchoolNews where schoolid in (select schoolid from school where schoolname = '" + self.lbt_schoolname_text + "') and status =1 order by id desc "
        querytitle = self.selectlbt(sqltitle)
        print(querytitle+"===="+newtitle)
        self.assertTrue(querytitle == newtitle)
        sqlcontent = "select top 1 content from C_SchoolNews where schoolid in (select schoolid from school where schoolname = '" + self.lbt_schoolname_text + "') and status =1 order by id desc "
        querycontent = self.selectlbt(sqlcontent)
        querycontent = BeautifulSoup(querycontent).get_text().replace(" ", "")
        self.assertTrue(querycontent == newtext.replace(" ", ""))
        """删除刚才新增的那个新闻,并校验是否删除成功"""
        newslist = dr.find_elements_by_css_selector(self.schoolstation_dynamic_newslist_css)
        if newslist.__len__() > 0:
            addtitle = newslist[0].find_element_by_tag_name(self.schoolstation_dynamic_newsfirsttitle_tag).text
            self.assertTrue(addtitle == newtitle)
            newslist[0].find_element_by_tag_name(self.schoolstation_dynamic_newsfirstdelete_tag).click()
            dr.find_element_by_class_name(self.schoolstation_dynamic_delete_class).click()
            sqltitle = "select count(1) from C_SchoolNews where schoolid in (select schoolid from school where schoolname = '" + self.lbt_schoolname_text + "') and status =1 and title = '" + newtitle + "' "
            titlecount = self.selectlbt(sqltitle)
            self.assertTrue(int(titlecount) == 0)

    @lbtlogin.loginFun
    def test_editdynamic(self):
        """
        编辑园所动态并页面校验，然后改回来
        :return:
        """
        self.docstr ='编辑园所动态并页面校验，然后改回来'
        dr = self.driver
        self.moduleOp.openMoudleAndIn(self.schoolstation_navigation_class, self.schoolstation_dynamic_url)
        newslist = dr.find_elements_by_css_selector(self.schoolstation_dynamic_newslist_css)
        if newslist.__len__() > 0:
            """如果之前已经有新闻了，那么先编辑成指定的标题，然后校验，然后再改回来，然后再校验"""
            oldtitle = newslist[0].find_element_by_tag_name(self.schoolstation_dynamic_newsfirsttitle_tag).text
            newslist[0].find_element_by_tag_name(self.schoolstation_dynamic_newsfirstedit_tag).click()
            newtitle = "新闻标题" + self.getCurrentTime()
            self.clearAndSendKeyForText(dr.find_element_by_css_selector(self.schoolstation_dynamic_publish_title_css),newtitle,dr)
            dr.find_element_by_xpath(self.schoolstation_dynamic_publish_publish_xpath).click()
            newslist = dr.find_elements_by_css_selector(self.schoolstation_dynamic_newslist_css)
            currenttitle = newslist[0].find_element_by_tag_name(self.schoolstation_dynamic_newsfirsttitle_tag).text
            self.assertTrue(newtitle == currenttitle)
            newslist[0].find_element_by_tag_name(self.schoolstation_dynamic_newsfirstedit_tag).click()
            self.clearAndSendKeyForText(dr.find_element_by_css_selector(self.schoolstation_dynamic_publish_title_css), oldtitle,dr)
            dr.find_element_by_xpath(self.schoolstation_dynamic_publish_publish_xpath).click()
            newslist = dr.find_elements_by_css_selector(self.schoolstation_dynamic_newslist_css)
            currenttitle = newslist[0].find_element_by_tag_name(self.schoolstation_dynamic_newsfirsttitle_tag).text
            self.assertTrue(oldtitle == currenttitle)
